home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter08 / RotateMoveZoomPanel.java < prev    next >
Text File  |  2000-07-03  |  3KB  |  127 lines

  1.  
  2.  
  3.  
  4. package applets;
  5.  
  6. import shout3d.*;
  7. import shout3d.core.*;
  8. import shout3d.math.*;
  9.  
  10.  
  11. public class RotateMoveZoomPanel extends Shout3DPanel implements DeviceObserver{
  12.     
  13.     Viewpoint camera;
  14.     float fov;
  15.  
  16.     int pixelStartX;
  17.     int pixelStartY;
  18.     int pixelEndX;
  19.     int pixelEndY;
  20.  
  21.     float[] worldPos = new float[3];
  22.  
  23.     float [] eulers = new float [3];
  24.     float [] axisAngle = new float [4];
  25.     Quaternion q = new Quaternion();
  26.  
  27.  
  28.     public RotateMoveZoomPanel (Shout3DApplet applet){
  29.         super(applet);
  30.     }
  31.     
  32.         public void customInitialize() {
  33.  
  34.         addDeviceObserver(this,"MouseInput", null);
  35.  
  36.         camera = (Viewpoint) getCurrentBindableNode("Viewpoint");
  37.  
  38.         worldPos = camera.position.getValue();
  39.  
  40.         axisAngle = camera.orientation.getValue();
  41.         q.setAxisAngle(axisAngle);
  42.         q.getEulers(eulers);
  43.  
  44.         fov = camera.fieldOfView.getValue();
  45.             
  46.     }
  47.  
  48.  
  49.  
  50.     protected void finalize()  {
  51.  
  52.         removeDeviceObserver(this,"MouseInput");
  53.  
  54.     }
  55.  
  56.  
  57.     public boolean onDeviceInput(DeviceInput di, Object userData) {
  58.  
  59.         MouseInput mi = (MouseInput) di;
  60.  
  61.         switch (mi.which){
  62.             case MouseInput.DOWN:
  63.                 pixelStartX = mi.x;
  64.                 pixelStartY = mi.y;
  65.                 return true;                                        
  66.  
  67.  
  68.             case MouseInput.DRAG:
  69.  
  70.               //if left button used
  71.               if (mi.button == 0) {
  72.  
  73.                 pixelEndX = mi.x;
  74.                 pixelEndY = mi.y;
  75.                 int dragDistanceX = pixelEndX - pixelStartX;
  76.                 int dragDistanceY = pixelEndY - pixelStartY;
  77.                 pixelStartX = pixelEndX;
  78.                 pixelStartY = pixelEndY;
  79.                 
  80.  
  81.                 //ROTATION
  82.  
  83.                 float rotationDelta = -(dragDistanceX/30f);
  84.                 eulers[0] = eulers[0] + rotationDelta;
  85.                 q.setEulers(eulers);
  86.                 q.getAxisAngle(axisAngle);
  87.                 camera.orientation.setValue(axisAngle);
  88.  
  89.                 //TRANSLATION
  90.  
  91.                 float translationDelta = dragDistanceY * 2.0f;
  92.                 float[] vector = {0,0, translationDelta};
  93.                 q.xform(vector);
  94.                 worldPos[0] = worldPos[0] + vector[0];
  95.                 worldPos[2] = worldPos[2] + vector[2];
  96.                 camera.position.setValue(worldPos);
  97.     
  98.                 return true;
  99.  
  100.               }//end of 0 if
  101.  
  102.  
  103.               //if right button used
  104.               if (mi.button == 1)  {
  105.  
  106.                 pixelEndY = mi.y;
  107.                 int dragDistanceY = pixelEndY - pixelStartY;
  108.                 
  109.                 //adjust field of view at
  110.                 //150 pixels dragged per 
  111.                 //radian (57.2 degrees)
  112.                 float fovDelta = dragDistanceY/150.0f;
  113.                 fov = fov + fovDelta;
  114.                 
  115.                 camera.fieldOfView.setValue(fov);
  116.  
  117.                 pixelStartY = pixelEndY;
  118.                 return true;
  119.  
  120.               }//end of 1 if
  121.  
  122.         }//end of switch
  123.  
  124.         return false;        
  125.     }
  126.  
  127. } //end of class